package com.hww.acommon.test;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.hww.acommon.R;
import com.hww.common.MyAppThis;
import com.hww.common.util.BitmapGrayUtil;

/**
 * @author hww
 * @date 0014 2024/6/14
 * email: heww@zhongyuntech.cn
 * description:
 */
public class BitmapGrayUtilsActivity extends BaseActivity {

//    private ActivityBaseBinding binding;

//    @Override
//    protected void onCreate(@Nullable Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        binding = DataBindingUtil.setContentView(this, R.layout.activity_base);
//
//        binding.bt1.setText("图片处理");
//
//
//    }

    @Override
    protected void initDate() {
        setGONE();
        binding.bt1.setVisibility(View.VISIBLE);
    }

    public void onClickBT1(View view) {
        test();
    }


    private void test() {
        new Thread(new Runnable() {
            @Override
            public void run() {

                /*Bitmap bitmap = BitmapFactory.decodeResource(MyAppThis.getThis().getResources(), R.mipmap.ic_launcher_round);*/
                Bitmap bitmap = BitmapFactory.decodeResource(MyAppThis.getThis().getResources(), R.drawable.one);

                showImage("原图", bitmap);

                Bitmap b1 = Bitmap.createBitmap(bitmap);
                Bitmap b11 = BitmapGrayUtil.bitmap2GrayBitmap(b1);
                showImage("图片灰度化", b11);

                Bitmap b2 = Bitmap.createBitmap(bitmap);
                Bitmap b21 = BitmapGrayUtil.bitmap2LightBitmap(b2);
                showImage("提高图片亮度", b21);

                Bitmap b3 = Bitmap.createBitmap(bitmap);
                Bitmap b31 = BitmapGrayUtil.bitmap2OTSUBitmap(b3);
                showImage("OTSU 算法获取二值图", b31);

                Bitmap b4 = Bitmap.createBitmap(bitmap);
                Bitmap b41 = BitmapGrayUtil.grayAverageBitmap2BinaryBitmap(b4);
                showImage("平均灰度算法获取二值图", b41);

                Bitmap b5 = Bitmap.createBitmap(bitmap);
                Bitmap b51 = BitmapGrayUtil.grayBitmap2BinaryBitmap(b5, true);
                showImage("亮度分界二值化算法", b51);

                System.out.println(00000);

            }
        }).start();
    }

    private void showImage(final String con, final Bitmap bitmap) {
        int widthTV = ViewGroup.LayoutParams.WRAP_CONTENT;
//        int width = ViewGroup.LayoutParams.MATCH_PARENT;
        int width = 300;
        ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(width, width);
        final ImageView iv = new ImageView(this);
        iv.setLayoutParams(layoutParams);


        ViewGroup.LayoutParams layoutParamsTV = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        final TextView textView = new TextView(this);
        textView.setLayoutParams(layoutParamsTV);
        BitmapGrayUtilsActivity.this.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                iv.setImageBitmap(bitmap);
                textView.setText(con);
                binding.baseLinearLayout.addView(iv);
                binding.baseLinearLayout.addView(textView);
            }
        });
    }
}
